Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

$location = [Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath
$solutionName = "EFSPWFActivities.ShrinkImage.wsp"

Write-Host " "
$sol = Get-SPSolution | Where-Object {$_.Name -eq $solutionName}
if ($sol -ne $null) {
	if($sol.Deployed -eq $true)
	{	
		Uninstall-SPSolution -Identity $solutionName -Confirm:$false
		write-host "uninstalling $($solutionName) ..." -NoNewline
		do 
		{ 
			Write-Host "." -NoNewline
			Start-Sleep 1
		}
		while ((Get-SPSolution $solutionName).JobExists -eq $true)
		Write-Host "ok"
	}
	else
	{
		Write-Host "Solution $($solutionName) not deployed"
	}
	
	if($sol.Deployed -eq $false)
	{
		write-host "removing $($solutionName) ..."
		Remove-SPSolution -Identity $solutionName -Confirm:$false
	}
	else
	{
		$sol2 = Get-SPSolution $solutionName
		Write-Host "test"
	}
}
else
{
	Write-Host "Solution $($solutionName) not installed"
}

Write-Host " "
$sol = Get-SPSolution | Where-Object {$_.Name -eq $solutionName}
if($sol -eq $null)
{
	Add-SPSolution $location\$solutionName
	write-host "Adding $($solutionName) ..." -NoNewline
	do 
	{ 
		Write-Host "." -NoNewline
		Start-Sleep 1
	}
	while ((Get-SPSolution $solutionName).JobExists -eq $true)
	Write-Host "ok"
}
else
{
	Write-Host "Solution $($solutionName) already installed"
}

$sol = Get-SPSolution | Where-Object {$_.Name -eq $solutionName}
if ($sol -ne $null) {
	if($sol.Deployed -eq $false)
	{	
		Install-SPSolution -Identity $solutionName -GACDeployment -Force
		write-host "Installing $($solutionName) ..." -NoNewline
		do 
		{ 
			Write-Host "." -NoNewline
			Start-Sleep 1
		}
		while ((Get-SPSolution $solutionName).JobExists -eq $true)
		Write-Host "ok"
	}
	else
	{
		Write-Host "Solution $($solutionName) already deployed."
	}
}
else
{
	Write-Host "Solution $($solutionName) was not installed, cannot deploy."
}
Write-Host " "
Read-Host "Completed.  Any key to exit."
